<template>
  <div class="category-list">
    <!-- <pre>{{categoryList}}</pre> -->
    <div class="list" v-for="(item, index) in categoryList" :key="index">
      <div class="item" @click="SwitchChange(item)">
        <span class="name">{{item.cat_name}}</span>
        <span class="arrow-right van-icon" @click.stop="SwitchAccount(item,index)" :class="{'van-icon-arrow': !item.isShow, 'van-icon-arrow-down':item.isShow}"></span>
      </div>
      <div class="children-item van-hairline-j--top" :class="{active: item.isShow}" v-if="item.isShow && item.children && item.children.length > 0">
        <store-category v-model="item.children" @change="SwitchChange" />
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: 'StoreCategory',
  data () {
    return {
      isShow: false
    };
  },
  props: {
    value: {
      type: Array
    }
  },
  computed: {
    categoryList: {
      get: function () {
        return this.value;
      },
      set: function (val) {
        this.$emit('input', val);
      }
    }
  },
  methods: {
    SwitchChange (id) {
      this.$emit('change', id);
    },
    SwitchAccount (item, index) {
      if (item.isShow) {
        this.categoryList[index].isShow = false;
        return;
      }
      this.$toast.loading({ duration: 0 });
      this.$API.store.stroeCategory({ pid: item.id, sid: this.$route.query.id }).then(res => {
        if (!res.code) {
          this.categoryList[index].isShow = !this.categoryList[index].isShow;
          this.isShow = true;
          this.categoryList[index].children = res.data.map((item) => {
            item.isShow = false;
            return item;
          });
          this.$toast.clear();
        } else {
          this.$toast(res.message);
          this.$toast.clear();
        }
      });
    }
  }
};
</script>
<style lang="less" scoped>
.category {
  .list {
    line-height: 88px;
    background-color: #fff;
    border-radius: 10px;
    padding: 0 20px 0 47px;
    margin-top: 20px;
    .item {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
    .name {
      font-size: 28px;
      color: #000;
      position: relative;
      font-weight: 800;
      &::before {
        content: '';
        display: block;
        width: 3px; /*no*/
        height: 26px;
        background: #ff0034;
        border-radius: 3px;
        position: absolute;
        left: -20px;
        top: 30px;
      }
    }
    .arrow-right {
      width: 15px;
      height: 25px;
      font-size: 28px;
      font-weight: bold;
      margin-right: 5px;
    }
  }
}
.children-item {
  height: 0;
  margin: 0 -20px 0 -20px;
  transition: height ease 0.3s;
  .list {
    margin: 0;
  }
}
.children-item.active {
  height: auto;
}

.active.value {
  transition: transform ease 0.3s;
}
</style>
